import random
import uuid
from Autotests.Suites.StressTest.Operations.CommonOperations import CommonOperations
from Config.RunConfig import RunConfig, Options


class StorageNodeOperations:
    def __init__(self, *args, **kwargs):
        self._operationsList = [
            self._enumerateStorageNodesByAccountId,
            self._enumerateStorageNodes,
        ]
        if  RunConfig().get(Options.StressTest.stressAllPartners) == 'True':
            self._operationsList.append(self._updateStorageNodeMode)
            self._operationsList.append(self._updateStorageNodeState)
            self._operationsList.append(self._updateStorageNode)
            self._operationsList.append(self._relocateAccountNode)

        self._baseStorageNode = {
            'name': 'StorageNode',
            'storageId': 1,
            'family': 'WEBDAVS',
            'user': 'NodeUser',
            'password': 'passowrd',
            'host': '127.0.0.1:443',
            'gatewayHost': '11999'
        }
        self._storageNodeState = [
            'Online',
            'Offline',
            'Full',
            'Migrated'
        ]
        self._storageNodeMode = [
            'Operable',
            'OutOfService'
        ]
        self._storageNodesCount = 5

    def _getRandomOperation(self):
        return random.choice(self._operationsList)

    def _createStorageNode(self, manserv, storageNodeNumber):
        self._baseStorageNode['name'] = 'StorageNode_' + str(storageNodeNumber)
        manserv.AddStorageNode(**self._baseStorageNode)

    def _updateStorageNodeMode(self, manserv, partnerId):
        storageNodeResult = manserv.EnumerateStorageNodes(storageId = 1)
        modeIndex = random.randint(0, len(self._storageNodeMode) - 1)
        nodesCount = len(storageNodeResult.storageNodeInfo)
        randomNodeIndex = random.randint(1, nodesCount - 1)
        randomNodeId = storageNodeResult.storageNodeInfo[randomNodeIndex].id
        try:
            if random.randint(1, 100) % CommonOperations()._baseGrowthRate == 0:
                manserv.UpdateStorageNodeMode(storageNodeId = randomNodeId, mode = self._storageNodeMode[modeIndex])
        except:
            pass

    def _updateStorageNodeState(self, manserv, partnerId):
        storageNodeEnumerator = manserv.EnumerateStorageNodes(storageId = 1)
        stateIndex = random.randint(0, len(self._storageNodeState) - 1)
        nodesCount = len(storageNodeEnumerator.storageNodeInfo)
        randomNodeIndex = random.randint(1, nodesCount - 1)
        randomNodeId = storageNodeEnumerator.storageNodeInfo[randomNodeIndex].id
        try:
            if random.randint(1, 100) % CommonOperations()._baseGrowthRate == 0:
                manserv.UpdateStorageNodeState(storageNodeId = randomNodeId, state = self._storageNodeState[stateIndex])
        except:
            pass

    def _relocateAccountNode(self, manserv, partnerId):
        storageNodeEnumerator = manserv.EnumerateStorageNodes(storageId = 1)
        nodesCount = len(storageNodeEnumerator.storageNodeInfo)
        randomNodeIndex = random.randint(1, nodesCount - 1)
        randomNodeId = storageNodeEnumerator.storageNodeInfo[randomNodeIndex].id
        accountId = random.randint(1, 20000)
        try:
            accountData = manserv.GetAccountInfoById(accountId = accountId, includeHomeNodeInfo = 'true')
            manserv.RelocateAccountNode(accountId = accountData.accountInfo.id,
                fromStorageNodeId = accountData.homeNodeInfo.id, toStorageNodeId = randomNodeId)
        except:
            pass

    def _enumerateStorageNodesByAccountId(self, manserv, partnerId):
        accounts = manserv.EnumerateAccounts(partnerId = partnerId)
        if accounts:
            accountId = random.randint(1, len(accounts.accountInfo))
            manserv.EnumerateStorageNodesByAccountId(accounts = accounts.AaccountInfo[accountId].id)

    def _enumerateStorageNodes(self, manserv, partnerId):
        manserv.EnumerateStorageNodes(storageId = 1)

    def _updateStorageNode(self, manserv, partnerId):
        storageNodeEnumerator = manserv.EnumerateStorageNodes(storageId = 1)
        nodesCount = len(storageNodeEnumerator.storageNodeInfo)
        randomNodeIndex = random.randint(1, nodesCount - 1)
        randomNodeId = storageNodeEnumerator.storageNodeInfo[randomNodeIndex].id
        manserv.UpdateStorageNode(storageNodeId = randomNodeId, user = uuid.uuid4())
